/**
 * fileName:  RegionDictImpl.java
 * author:  lr
 * Company:  Cloudland Studio
 * Createdate:  2014-7-8 下午05:03:29
 *
 * All rights Reserved, Designed By cloudland Copyright(C) 2010-2011
 *   
 * Modification  History:
 *     Date           Author        Version        
 * -------------------------------------------
 * 2014-7-8          lr            1.0        
 *
 * Why & What is modified:
 * 1.0: 文件创建
 */
package org.cloudland.commercial.masterdata.handler.support;

import java.util.List;

import org.cloudland.commercial.masterdata.bean.FruitCategoryInfo;
import org.cloudland.commercial.masterdata.bean.RegionDictInfo;
import org.cloudland.commercial.masterdata.handler.RegionDictStore;
import org.cloudland.commercial.masterdata.store.entity.CategoryEntity;
import org.cloudland.commercial.masterdata.store.entity.RegionEntity;
import org.cloudland.commercial.masterdata.store.read.RegionDictReader;
import org.cloudland.commercial.masterdata.store.write.RegionDictWriter;

/**
 * <pre>
 * 查询地区字典接口
 * 使用 java.lang.Object#toString(), 或者 {@link java.lang.Character Character} 等标签
 * </pre>
 * 
 * @ClassName RegionDictImpl
 * @author lr
 * @version %I%, %G%
 * @see
 * @since JDK1.6
 * 
 */
public class RegionDictImpl implements RegionDictStore {

	/**
	 * regionDictWriter 地区更新操作
	 */
	private RegionDictWriter regionDictWriter;

	/**
	 * regionDictReader 地区查询操作
	 */
	private RegionDictReader regionDictReader;

	public RegionDictImpl(RegionDictWriter regionDictWriter,
			RegionDictReader regionDictReader) {
		this.regionDictWriter = regionDictWriter;
		this.regionDictReader = regionDictReader;
	}

	/**
	 * @return
	 */
	public RegionDictInfo createRegion() {
		RegionDictInfo categoryInfo = new RegionDictBean(regionDictWriter,
				regionDictReader);
		return categoryInfo;
	}

	/**
	 * @param regionId
	 * @return
	 */
	public RegionDictInfo findRegion(String regionId) {
		RegionEntity entity = regionDictReader.findById(regionId);
		RegionDictBean bean = new RegionDictBean(entity, regionDictWriter,
				regionDictReader);

		return bean;
	}

	/**
	 * @param regionId
	 */
	public void remove(String regionId) {
		RegionEntity entity = regionDictReader.findById(regionId);
		if (null == entity || entity.getRegionId().length() <= 0) {
			return;
		}
		regionDictWriter.delete(entity);
	}

	/**
	 * @return
	 */
	public RegionDictInfo[] findRootRegion() {
		List<RegionEntity> entityArray = regionDictReader.findRegionRoot();
		if (null == entityArray) {
			return null;
		}

		RegionDictInfo[] resultArray = new RegionDictInfo[entityArray.size()];
		for (int i = 0; i < entityArray.size(); i++) {
			resultArray[i] = new RegionDictBean(entityArray.get(i),
					regionDictWriter, regionDictReader);
		}
		return resultArray;
	}

}
